<?php

namespace app\common\model\integral;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'integral_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'images_array',
        'image_str',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : $data['image'];
    }
    public function getImagesArrayAttr($value, $data)
    {
        if ($data['images']) {
            $array = explode(',', $data['images']);
            foreach ($array as &$v) {
                $v = cdnurl($v, true);
            }
            return $array;
        }
        return [];
    }
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function category()
    {
        return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    /**
     * 判断商品是否能下单
     * @param int $product_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function checkGoods($data)
    {
        $goods = $data['integralgoods'];
        if (!isset($goods) || !$goods) {
            exception('商品【' . $goods['name'] . '】不存在');
        }
        if ($goods['stock'] < $data['num']) {
            exception('商品【' . $goods['name'] . '】库存不足，不能下单');
        }
        if ($goods['status'] == '0') {
            exception('商品【' . $goods['name'] . '】已下架，不能下单');
        }
        if ($goods['deletetime'] != null) {
            exception('商品【' . $goods['name'] . '】已删除，不能下单');
        }
    }

    /**
     * 增销减库
     * @param int $product_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function reduceInventory($goods_id, $num)
    {

        try {
            $goods = Goods::get($goods_id);
            if ($goods->stock < $num) {
                exception('库存不足');
            }
            $goods->setInc('sales', $num);
            $goods->setDec('stock', $num);
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
    /**
     * 减销增库
     * @param int $goods_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function stockUp($goods_id, $num)
    {
        try {
            $goods = Goods::get($goods_id);
            if ($goods->stock < $num) {
                exception('库存不足');
            }
            $goods->setDec('sales', $num);
            $goods->setInc('stock', $num);
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
        return true;
    }
}
